package com.example.entreamigoslocal;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class CadastroActivity extends Activity {
	BdCadastro dbHelper = null;
	SQLiteDatabase db = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cadastro);
		
		dbHelper = new BdCadastro(getApplicationContext());
				
	}
	
	public void cadastroUsuario(View view){
    	try{
    		EditText nome = (EditText) findViewById(R.id.editTextNome);
    		String nomeCompleto = nome.getText().toString();
    		
    		EditText usuario = (EditText) findViewById(R.id.editTextUsuario);
    		String nomeUsuario = usuario.getText().toString();
    		
    		EditText senha = (EditText) findViewById(R.id.editTextSenha);
    		String senhaDigitada = senha.getText().toString();
    		
    		db = dbHelper.getWritableDatabase();
    		
    		ContentValues values = new ContentValues();
    		values.put("NM_COMPLETO_USUARIO", nomeCompleto);
    		values.put("NM_USUARIO", nomeUsuario);
    		values.put("NM_SENHA_USUARIO", senhaDigitada);
    		
    		long resultado = db.insert("USUARIO", null, values);
    		
    		if(resultado != -1){
    			Toast.makeText(getApplicationContext(), "Cadastro efetuado com sucesso!", Toast.LENGTH_LONG).show();
    		}else{
    			Toast.makeText(getApplicationContext(), "Falha ao cadastrar usuario", Toast.LENGTH_LONG).show();
    		}
    		db.close();
    		dbHelper.close();
    		    		
    		Intent i = new Intent(this, LoginActivity.class);
    		startActivity(i);
    		finish();     		
        		
        	   		    		
    	}catch(Exception ex){
    		Toast.makeText(getApplicationContext(), "Erro no cadastro", Toast.LENGTH_LONG).show();
        	
        	Intent i = new Intent(this, CadastroActivity.class);
        	startActivity(i);
    	}
    }
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.cadastro, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
